package com.wxxymaker.edu.controller;

import com.blade.exception.BladeException;
import com.blade.ioc.annotation.Bean;
import com.blade.mvc.WebContext;
import com.blade.mvc.handler.DefaultExceptionHandler;
import com.blade.mvc.ui.RestResponse;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @AUTHOR soft
 * @DATE 2018/10/6 22:19
 * @DESCRIBE 全局异常处理
*/

@Bean
@Slf4j
public class GExceptionHandler extends DefaultExceptionHandler {

    @Override
    public void handle(Exception e) {
        if (e instanceof NullPointerException) {
            log.error("空指针异常: {}", e.getMessage());
        } else {
            if (e instanceof IOException) {
                if (e.getMessage().contains("你的主机中的软件中止了")) {
                    log.error("忽略：{}", e.getMessage());
                }
            } else {
                int status = 500;
                String name = e.getMessage();
                if (e instanceof BladeException) {
                    status = ((BladeException) e).getStatus();
                    name = ((BladeException) e).getName();
                    super.handle(e);
                } else {
                    log.error("内部错误! {} {}", status, name);
                    WebContext.response().json(RestResponse.fail("Code: " + status + " Msg: " + name));
                }
            }
        }
    }
}
